package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.SystemRegionDao;
import com.example.child_protection.dto.info.SystemRegionDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.SystemMenuRequestDTO;
import com.example.child_protection.dto.request.find.SystemRegionRequestDTO;
import com.example.child_protection.dto.response.SystemMenuListResponseDTO;
import com.example.child_protection.dto.response.SystemRegionListResponseDTO;
import com.example.child_protection.dto.response.SystemRegionResponseDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.SystemRegionModel;
import com.example.child_protection.service.SystemRegionQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 科室查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class SystemRegionQueryServiceImpl extends MyBaseService implements SystemRegionQueryService {

    @Autowired
    public SystemRegionDao systemRegionDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public SystemRegionResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.REGION_FIND_ID, requestDTO, SystemRegionResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, SystemRegionResponseDTO>() {
            @Override
            protected SystemRegionResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                SystemRegionResponseDTO responseDTO = new SystemRegionResponseDTO();
                SystemRegionModel systemRegionModel = systemRegionDao.findId(requestDTO.getId());
                SystemRegionDTO systemRegion = new SystemRegionDTO();
                BeanUtils.copyProperties(systemRegionModel, systemRegion);
                responseDTO.setInfo(systemRegion);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public SystemRegionListResponseDTO findList(SystemRegionRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.REGION_FIND_LIST, requestDTO, SystemRegionListResponseDTO.class, new MyServiceExecutor<SystemRegionRequestDTO, SystemRegionListResponseDTO>() {
            @Override
            protected SystemRegionListResponseDTO myprocess(SystemRegionRequestDTO requestDTO) throws Exception {
                SystemRegionListResponseDTO responseDTO = new SystemRegionListResponseDTO();
                SystemRegionModel req = new SystemRegionModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<SystemRegionModel> lists = systemRegionDao.findPage(req);
                List<SystemRegionDTO> roles = JSONObject.parseArray(JSONArray.toJSONString(lists), SystemRegionDTO.class);
                responseDTO.setInfos(roles);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }
}
